﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LCUV;
using System.Data;

namespace sWarranty.WEB
{
    public partial class ProductRegPndQue : System.Web.UI.Page
    {
        public string _currentUserEmpNo = "";
        public string _currentUserAuthority = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            _currentUserEmpNo = GetUserLoginEmpNo();
            Session["UrltoFw"] = "ProductRegPndQue.aspx";

            if (_currentUserEmpNo == "")
                Response.Redirect("login.aspx");

            string sn = "";
            try
            {
                sn = Request.QueryString["sn"].ToString();
            }
            catch (Exception)
            {
            }
            if (!IsPostBack)
                if (!string.IsNullOrEmpty(sn))
                {
                    DoFind(txtModel.Text.Trim(), sn );
                }
        }
        protected string GetUserLoginEmpNo()
        {
            string t = "";
            try
            {
                if (Session["UserLoginInfo"] != null)
                {
                    Entities db = new Entities();
                    var u = (TB_USER[])Session["UserLoginInfo"];
                    int _UID = Convert.ToInt32(u.Single().ID);
                    var au = from ft in db.TB_USER_LEVEL
                             where ft.USER_ID == _UID
                             select ft;
                    int _le = Convert.ToInt32(au.Single().LEVEL_ID);
                    var le = from xx in db.TB_LEVEL
                             where xx.ID == _le
                             select xx;
                    _currentUserAuthority = le.Single().REMARK;
                    return u.Single().EMPNO;
                }
            }
            catch (Exception)
            {
                t = "";
            }
            return t;
        }

        private void DoFind(string p_model, string p_serial_no)
        {
            litData.Text = "";
            string md = null;
            string sn = null;
            string box = null;
            if (!string.IsNullOrEmpty(p_model))
                md = p_model;
            if (!string.IsNullOrEmpty(p_serial_no))
                sn = p_serial_no;
            if (string.IsNullOrEmpty(sn))
            {
                MessageBox.Show("You must input Serial No");
                return;
            }
            try
            {
                DataTable dt = new DataTable();
                dt = DBAction.runStore("PK_SOM_MST.PR_SN_TRC", "uiroi", "p_serial_no", sn, "p_model", md,
                    null, null, null, null, null, null, null, null, null, null, null, null, null, null
                    , null, null, null, null, null, null, null, null, null, null, null, null);
                try
                {
                    if (dt.Rows.Count > 0 && !string.IsNullOrEmpty(dt.Rows[0]["Error"].ToString()))
                    {
                        MessageBox.Show(dt.Rows[0]["Error"].ToString());
                        return;
                    }
                }
                catch (Exception)
                {
                    //Column 'Error' does not belong to table <-- ko co' loi j va co' data , thi ko lam j
                }
                int i = 1;
                int totalrow = dt.Rows.Count;
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow item in dt.Rows)
                    {
                        litData.Text += "<tr id='tr" + i.ToString() + "'>";
                        litData.Text += "   <td>" + i.ToString() + "</td>";
                        litData.Text += "   <td>" + item["SERIAL_NO"].ToString() + "</td>";
                        litData.Text += "   <td>" + item["BOX_NO"].ToString() + "</td>";
                        txtModel.Text = item["PROD_MODEL"].ToString();
                        litData.Text += "   <td>" + item["PROD_MODEL"].ToString() + "</td>";
                        litData.Text += "   <td>" + item["PROD_SUFFIX"].ToString() + "</td>";
                        txtSuffix.Text = item["PROD_SUFFIX"].ToString();
                        litData.Text += "   <td>" + item["SAL_MODEL"].ToString() + "</td>";
                        litData.Text += "   <td>" + item["SAL_SUFFIX"].ToString() + "</td>";
                        litData.Text += "   <td>" + item["ORDER_NO"].ToString() + "</td>";
                        litData.Text += "   <td>" + Convert.ToDateTime(item["PRODUCT_DATE"].ToString()).ToString("yyyy-MMM-dd") + "</td>";
                        litData.Text += "   <td>" + item["SELLIN_DATE"].ToString() + "</td>";
                        litData.Text += "   <td><span><input id='btnConfirm" + i.ToString() + "' runat='server' type='button' class='button float-right' tittle='Find Pick order(s)' value='Order' onclick=\"findToConfirm('" + totalrow.ToString() + "','" + i.ToString() + "','" + item["SERIAL_NO"].ToString() + "','" + item["BOX_NO"].ToString() + "','" + item["PROD_MODEL"].ToString() + "','" + item["PROD_SUFFIX"].ToString() + "','" + Convert.ToDateTime(item["PRODUCT_DATE"].ToString()).ToString("yyyyMMdd") + "','" + _currentUserEmpNo + "')\" ></span></td>";
                        litData.Text += "</tr>";
                        i++;
                    }
                    if (i >= 3)
                    {
                        //txtSN.Enabled = true;
                        //btnFind.Enabled = true;
                    }
                    //ClientScript.RegisterClientScriptBlock(this.GetType(), "FormatTable",@"$(document).ready(function() {FormatTable();});", true);

                }
                else
                {
                    //no data of PR_SN_TRC
                    //btnOrder.Enabled = false;
                    divData.Visible = false;
                    divData2.Visible = true;
                    litData2.Text = "";
                    try
                    {
                        string[] st = Session["ProductRegPNDAspxData"].ToString().Split('|');
                        //Session["ProductRegPNDAspxData"] = from.ToString("yyyyMMdd") + "|" + to.ToString("yyyyMMdd") + "|" + product + "|"
                        //    + model + "|" + serial + "|" + smsYN + "|" + pndType + "|" + shopName + "|" + regID + "|" + ureg;
                        dt = DBAction.runStore("PK_SOM_MST.PR_PROD_REG_PND_INQ", "uiroi", "p_ds_date", st[0].ToString(), "p_de_date",
                             st[1].ToString(), "p_product", st[2].ToString(), "p_model", md, "p_serial_no",
                             sn, "p_sms_yn", st[5].ToString(), "p_pnd_type", st[6].ToString(), "p_shop_name", st[7].ToString(), "p_region_id",
                             st[8].ToString(), "p_user_region", st[9].ToString(), null, null, null, null, null, null, null, null, null, null);//from.ToString("yyyyMMdd"), to.ToString("yyyyMMdd"), _BillCode, _product, _model));
                        try
                        {
                            if (dt.Rows.Count > 0 && !string.IsNullOrEmpty(dt.Rows[0]["Error"].ToString()))
                            {
                                MessageBox.Show(dt.Rows[0]["Error"].ToString());
                                return;
                            }
                        }
                        catch (Exception)
                        {
                            //Column 'Error' does not belong to table <-- ko co' loi j va co' data , thi ko lam j
                        }
                        if (dt.Rows.Count > 0)
                            foreach (DataRow item in dt.Rows)
                            {
                                if (item["REJECT_STATUS"].ToString().ToLower() == "n")
                                {
                                    litData2.Text += "<tr>";
                                    litData2.Text += "   <td> <span id='span" + item["SERIAL_NO"].ToString() + "'>" + item["REJECT_STATUS"].ToString() + "</span><img id='imgLoadingabc' src='../images/ajax-loader.gif' style='display:none' /></td>";
                                    litData2.Text += "   <td>" + item["SHOP_CODE"].ToString() + "</td>";
                                    litData2.Text += "   <td>" + item["PRODUCT"].ToString() + "</td>";
                                    litData2.Text += "   <td>" + item["MODEL"].ToString() + "</td>";
                                    litData2.Text += "   <td>" + item["SERIAL_NO"].ToString() + "</td>";
                                    litData2.Text += "   <td>" + item["SHOP_CELL"].ToString() + "</td>";
                                    litData2.Text += "   <td>" + item["ENDUSER_CELL"].ToString() + "</td>";
                                    litData2.Text += "   <td><input type='button' class='button float-right' onclick=\"setReject('" + item["SERIAL_NO"].ToString() + "','" + _currentUserEmpNo + "')\" id='btn" + item["SERIAL_NO"].ToString() + "' value='Reject' />       <input id='btnConfirm" + i.ToString() + "' runat='server' type='button' class='button float-right' tittle='Find Pick order(s)' value='Order' onclick=\"findToConfirm2('" + totalrow.ToString() + "','" + i.ToString() + "','" + item["SERIAL_NO"].ToString() + "','" + _currentUserEmpNo + "')\" ></td>";
                                    litData2.Text += "</tr>";
                                    i++;
                                }
                            }
                        else
                        {
                            litData2.Text = "No data";
                        }
                    }
                    catch (Exception ee)
                    {
                        MessageBox.Show(ee.Message);
                    }
                }
            }
            catch (Exception ee)
            {
                if (ee.InnerException != null)
                    MessageBox.Show(ee.InnerException.Message);
                else
                    MessageBox.Show(ee.Message);
            }
        }

        protected void btnFind_Click(object sender, EventArgs e)
        {
            //DoFind(txtModel.Text.Trim(), txtSN.Text.Trim());
        }

        protected void btnOrder_Click(object sender, EventArgs e)
        {
            //ClientScript.RegisterClientScriptBlock(Me.GetType(), "FormatTable",@"$(document).ready(function() {FormatTable();});", True);

        }
    }
}